package com.im.partchat.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.im.partchat.R;
import com.im.partchat.widget.NetworkStateView;
import com.im.partchat.widget.XLoadingDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by fzl on 2017/4/6.
 * VersionCode: 1
 * Desc:BaseFragment
 */

public abstract class BaseFragment extends Fragment implements IBaseView {
    public final String TAG = this.getClass().getSimpleName();
    protected NetworkStateView networkStateView;
    protected XLoadingDialog xLoadingDialog;
    protected Activity context;
    private Unbinder unbinder;
    private View mView;

    private ILoadingType.LoadingType loadingType;


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isRegisterEventBus() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        context = getActivity();
        loadingType = initLoadType();
        initView(view, savedInstanceState);
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_base, container, false);

        ViewGroup parent = (ViewGroup) mView.getParent();
        if (null != parent) {
            parent.removeView(mView);
        }
        addChildView(inflater);
        unbinder = ButterKnife.bind(this, mView);
        return mView;

    }

    /**
     * 添加子Fragment的布局文件
     */
    private void addChildView(LayoutInflater inflater) {
        networkStateView = (NetworkStateView) mView.findViewById(R.id.nsv_state_view);
        FrameLayout container = (FrameLayout) mView.findViewById(R.id.fl_fragment_child_container);
        View child = inflater.inflate(getLayoutId(), null);
        container.addView(child, 0);
    }

    protected abstract int getLayoutId();

    protected abstract void initData();

    protected void initView(View view, Bundle savedInstanceState) {
    }

    public void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showEmpty() {
        networkStateView.showEmpty();
    }

    public void showPageSuccess() {
        networkStateView.showSuccess();
    }


    protected ILoadingType.LoadingType initLoadType() {
        return ILoadingType.LoadingType.LOADING_TYPE_DIALOG;
    }

    @Override
    public void showLoading(String msg) {
        if (loadingType == ILoadingType.LoadingType.LOADING_TYPE_DIALOG) {
            if (xLoadingDialog == null) {
                xLoadingDialog = new XLoadingDialog(context);
                xLoadingDialog.setMessage(msg);
                xLoadingDialog.show();
            } else {
                xLoadingDialog.setMessage(msg);
                xLoadingDialog.show();
            }
        }else {
            networkStateView.showLoading();
        }
    }


    @Override
    public void hideLoading() {
        if (loadingType == ILoadingType.LoadingType.LOADING_TYPE_DIALOG) {
            if (xLoadingDialog != null) {
                xLoadingDialog.dismiss();
            }
        }else {
            networkStateView.showSuccess();
        }
    }

    @Override
    public void showError(String error) {
        showToast(error);
    }

    @Override
    public void onDestroy() {

        if (isRegisterEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        unbinder.unbind();
        super.onDestroy();

    }


    protected boolean isRegisterEventBus() {
        return false;
    }
}
